package cn.itsource.config;

import cn.itsource.domain.Logs;
import cn.itsource.service.ILogService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@Aspect
@Component
public class TeacherAop {
    @Autowired
    private HttpServletRequest httpServletRequest;
    @Pointcut("execution(public * cn.itsource.service.I*Service.*(..)) && !execution(public * cn.itsource.service.ILogService.*(..))")
    public void pointcut() {

    }
    @Autowired
    private ILogService iLogService;

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) {
//        Logs logs = new Logs();
//
//        logs.setUserName("root");
//
//        logs.setUserId(123456L);
//
//        logs.setClassName( proceedingJoinPoint.getTarget().getClass().getName());
//
//        logs.setMethodName(proceedingJoinPoint.getSignature().getName());
//
//        logs.setMethodParams(Arrays.toString(proceedingJoinPoint.getArgs()));
//
//        long beginTime = System.currentTimeMillis();
        Object result = null;
        try {
            result = proceedingJoinPoint.proceed();
//            logs.setReturnValue(result==null?null:result.toString());
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
//        logs.setCostTime(System.currentTimeMillis() - beginTime);
//        logs.setIp(httpServletRequest.getRemoteAddr());
//        System.out.println(logs);
//        iLogService.save(logs);
        return result;
    }
}
